package com.lpf.nio;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * 功能描述：文件锁
 *
 * @author lpf
 * @date 2019/11/25 22:45
 * @version: 1.0
 **/
public class NioTest10 {

    public static void main(String[] args) throws Exception {

        RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\LIUPEIFENG\\IdeaProjects\\Netty-Learning\\netty-zhanglong\\src\\main\\java\\com\\lpf\\nio\\text\\NioTest10.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        /**
         *lock(3,6,true); 3：开始锁的位置  6：锁的长度  true：共享锁（false 排它锁）
         */
        FileLock fileLock = fileChannel.lock(3, 6, true);

        System.out.println("vaild: " + fileLock.isValid());
        System.out.println("lock type: " + fileLock.isShared());

        fileLock.release();

        randomAccessFile.close();
    }
}
